import { getInfo, getMenu } from "@renderer/api/user";
import { IUserInfo, Parent, Role } from "@renderer/interface/user";
import { defineStore } from "pinia";

// 创建并暴露用户仓库
export const useUserStore = defineStore('user', {
	state: (): {
		roles: Role[]
		rolePerm: string
		menu: Parent[]
		userInfo: IUserInfo,
		permissions: string[]
	} => {
		return {
			roles: [],
			rolePerm: '',
			menu: [],
			userInfo: {} as IUserInfo,
			permissions: []
		}
	},
	actions: {
		// 获取用户信息
		async getUserInfo() {
			const res = await getInfo()
			const { permissions, roles, units, userInfo } = res.data
			this.roles = roles
			this.rolePerm = roles[0].rolePerm
			this.userInfo = userInfo
			this.permissions = permissions
		},
		//获取用户路由权限
		async getUserMenu() {
			const res = await getMenu(this.rolePerm)
			this.menu = res.data
		}
	},
	// 持久化存储
	persist: {
		enabled: true, // 开启数据缓存
		strategies: [
			{
				storage: localStorage, // 默认存session
				paths: ['rolePerm', 'menu', 'userInfo', 'permissions']
			}
		]
	}
})